package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.KlassMapper;
import com.group3.dao.StudentInfoMapper;
import com.group3.domain.*;
import com.group3.pojo.receive.KlassReceive;
import com.group3.pojo.receive.StudentReceive;
import com.group3.pojo.result.KlassResult;
import com.group3.service.AClassService;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Service
public class AClassServiceImpl implements AClassService {

    @Resource
    private KlassMapper klassMapper;
    @Resource
    private StudentInfoMapper studentInfoMapper;

    @Override
    @Transactional
    public Result addClass(KlassReceive klassReceive) {

        Klass klass = new Klass(klassReceive);

        int rs = klassMapper.insertSelective(klass);

        return rs > 0 ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
    }

    @Override
    @Transactional
    public Result<List<KlassResult>> selectClass(KlassReceive klassReceive) {

        KlassExample klassExample = new KlassExample();
        KlassExample.Criteria criteria = klassExample.createCriteria();

        if (NotNull.judge(klassReceive.getDepartName())) {
            criteria.andDepartNameEqualTo(klassReceive.getDepartName());
        }
        if (NotNull.judge(klassReceive.getProfessionName())) {
            criteria.andProfessionNameEqualTo(klassReceive.getProfessionName());
        }
        if (NotNull.judge(klassReceive.getGrade())) {
            criteria.andGradeEqualTo(klassReceive.getGrade());
        }
        if (NotNull.judge(klassReceive.getClassId())) {
            criteria.andClassIdEqualTo(klassReceive.getClassId());
        }
        if (NotNull.judge(klassReceive.getTeacherId())) {
            criteria.andTeacherIdEqualTo(klassReceive.getTeacherId());
        }

        if (NotNull.judge(klassReceive.getStatue())) {
            criteria.andStateEqualTo(klassReceive.getStatue());
        }
        klassExample.setOrderByClause("class_id");

//        1.先开启分页
        PageHelper.startPage(klassReceive.getPageNumber(), klassReceive.getPageSize());//启动分页
//        2.执行SQL语句
        List<KlassResult> klassResults = klassMapper.selectResultByExample(klassExample);
//        3.获取分页信息
        PageInfo<KlassResult> klassResultsPageInfo = new PageInfo<>(klassResults);//获取分页信息

//        4.返回查询信息
        return ResultUtil.success("查询成功", klassResults, klassResultsPageInfo.getTotal());
    }

    @Override
    @Transactional
    public Result updateClass(KlassReceive klassReceive) {

        Klass klass = new Klass(klassReceive);

        int rs = klassMapper.updateByPrimaryKeySelective(klass);

        return rs > 0 ? ResultUtil.success("修改成功") : ResultUtil.error("修改失败");
    }

    @Override
    @Transactional
    public Result selectStudentByClass(StudentReceive studentReceive) {

        StudentInfo studentInfo = new StudentInfo(studentReceive);

        StudentInfoExample studentInfoExample = new StudentInfoExample();

        StudentInfoExample.Criteria criteria = studentInfoExample.createCriteria();
//        criteria.andClassIdEqualTo(studentInfo.getClassId() != null ? studentInfo.getClassId() : "")
//                .andStudentNameLike("%" + ((studentInfo.getStudentName() != null ? studentInfo.getStudentName() : "")) + "%")
//                .andStudentIdLike("%" + (studentInfo.getStudentId() != null ? studentInfo.getStudentId() : "") + "%")
//                .andCardIdLike("%" + (studentInfo.getCardId() != null ? studentInfo.getCardId() : "") + "%")
//                .andEmailLike("%" + (studentInfo.getEmail() != null ? studentInfo.getEmail() : "") + "%")
//                .andTelLike("%" + (studentInfo.getTel() != null ? studentInfo.getTel() : "") + "%");

        if (NotNull.judge(studentInfo.getStudentId())){
            criteria.andStudentIdEqualTo(studentInfo.getStudentId());
        }
        if (NotNull.judge(studentInfo.getStudentName())){
            criteria.andStudentNameLike("%"+(studentInfo.getStudentName())+"%");
        }
        if (NotNull.judge(studentInfo.getCardId())){
            criteria.andCardIdEqualTo(studentInfo.getCardId());
        }
        if (NotNull.judge(studentInfo.getEmail())){
            criteria.andEmailEqualTo(studentInfo.getEmail());
        }
        if (NotNull.judge(studentInfo.getTel())){
            criteria.andTelEqualTo(studentInfo.getTel());
        }
        if (NotNull.judge(studentInfo.getSex())){
            criteria.andSexEqualTo(studentInfo.getSex());
        }
        if (NotNull.judge(studentInfo.getType())){
            criteria.andTypeEqualTo(studentInfo.getType());
        }

        studentInfoExample.setOrderByClause("student_id");

//        1.先开启分页
        PageHelper.startPage(studentReceive.getPageNumber(), studentReceive.getPageSize());//启动分页
//        2.执行SQL语句
        List<StudentInfo> studentInfos = studentInfoMapper.selectByExample(studentInfoExample);
//        3.获取分页信息
        PageInfo<StudentInfo> StudentInfoPageInfo = new PageInfo<>(studentInfos);//获取分页信息

//        4.返回查询信息/
        return ResultUtil.success("查询成功", studentInfos, StudentInfoPageInfo.getTotal());

    }

    @Override
    @Transactional
    public Result addStudentByClass(StudentReceive studentReceive) {

        StudentInfo studentInfo = new StudentInfo(studentReceive);

        int rs = studentInfoMapper.insertSelective(studentInfo);

        return rs > 0 ? ResultUtil.success("添加成功") : ResultUtil.error("添加失败");
    }

    @Override
    @Transactional
    public Result deletStudent(List<String> studentIds) {

        StudentInfo studentInfo = new StudentInfo();
        studentInfo.setClassId(null);

        for (String studentId : studentIds) {

            StudentInfoExample studentInfoExample = new StudentInfoExample();
            studentInfoExample.createCriteria().andStudentIdEqualTo(studentId);
            int rs = studentInfoMapper.updateByExampleSelective(studentInfo, studentInfoExample);
            if (rs < 0) {
                return ResultUtil.error("删除失败");
            }
        }

        return ResultUtil.success("删除成功");
    }
}
